package com.wrench.elevator.install.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import wrench.elevator.install.util.WrenchElevatorInstallException;
import wrench.elevator.install.util.WrenchElevatorInstallUtil;

import com.wrench.elevator.install.constant.Dict;
import com.alibaba.fastjson.JSON;
import com.wrench.elevator.install.constant.Result;
import com.wrench.elevator.install.dao.UserDao;
import com.wrench.elevator.install.domain.User;


@Component
public class UserService
{
    
    
    @Resource
    private UserDao dao;
    
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    

    /**
     * 登录
     */
    public void userLogin(String tel, String pwd, ModelMap params)
        throws WrenchElevatorInstallException
    {
        
        if (WrenchElevatorInstallUtil.hasNull(tel,pwd, params))
        {
            logger.error("==> param is not found , tel = {} , pwd = {} , params = {} ",
                tel,
                pwd,
                params);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.isVaildPhone(tel))
        {
            logger.error("==> phone number is not vaild , tel = {} ", tel);
            throw new WrenchElevatorInstallException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.isEncode(pwd))
        {
            logger.error("==> password is not encode , user_pwd = {} ", pwd);
            throw new WrenchElevatorInstallException(Result.PASSWORD_IS_NOT_ENCODE_CODE, Result.PASSWORD_IS_NOT_ENCODE_MESSAGE);
        }
        
        
        params.put("tel", tel);
        params.put("pwd",pwd);
        
        try
        {
            dao.login(params);
        }
        catch (Exception e)
        {
            logger.error("==> dao.login(params) execute error , params = {} , error = {}  ",
                JSON.toJSONString(params),
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.login(params) execute success , params = {} ", JSON.toJSONString(params));
        
        params.remove("tel");
        params.remove("pwd");
        Object user_id = params.get("user_id");
        Object first = params.get("first");
        String result = (String)params.get("result");
        if (null == user_id)
        {
            
            logger.debug("==> login fail , user_id is null , user_id = {}", user_id);
            throw new WrenchElevatorInstallException(Result.TEL_HAS_NOT_REG_CODE, Result.TEL_HAS_NOT_REG_MESSAGE);
        }
        
        if (null == first)
        {
            params.put("first", "YES");
        }
        else 
        {
            params.put("first", "NO");
        }
        if (WrenchElevatorInstallUtil.objectEquals(Dict.PWDERROT, result))
        {
            params.clear();
            logger.debug("==> login fail , pwd is error , user_id = {}", user_id);
            throw new WrenchElevatorInstallException(Result.PWD_IS_ERROR_CODE, Result.PWD_IS_ERROR_MESSAGE);
        }
     }
    
    /**
     * 删除员工
     */
    public void delEmp(String user_id, String power, String emp_id,ModelMap params)
        throws WrenchElevatorInstallException
    {
        if (WrenchElevatorInstallUtil.hasNull(user_id,power, emp_id,params))
        {
            logger.error("==> param is not found , user_id = {} , power = {} , emp_id = {} , params = {}",
                user_id,
                power,
                emp_id,
                params);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchElevatorInstallUtil.areInt(user_id,emp_id))
        {
            logger.error("==> string is not int type , user_id = {} ,emp_id = {} ", user_id,emp_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        params.put("user_id", user_id);
        params.put("power", power);
        params.put("emp_id", emp_id);
        try
        {
            dao.delEmp(params);
        }
        catch (Exception e)
        {
            logger.error("==> dao.delEmp(params) execute error , params = {} ",user_id, power, emp_id,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.delEmp(params)) execute success , params = {} ", user_id, power, emp_id);
        
        params.remove("user_id");
        params.remove("power");
        params.remove("emp_id");
        
        Object result = params.get("result");
        if (WrenchElevatorInstallUtil.objectEquals(Dict.NOT_CAN_DEL, result))
        {
            logger.error("==> not can del emp , params = {} ", params);
            throw new WrenchElevatorInstallException(Result.NOT_CAN_DEL_CODE, Result.NOT_CAN_DEL_MESSAGE);
        }
    }
    
    /**
     * 加载用户列表
     */
    public void loadEmpList(String user_id, String power, ModelMap params)
    throws WrenchElevatorInstallException
    {
        if (WrenchElevatorInstallUtil.hasNull(user_id,power,params))
        {
            logger.error("==> param is not found , user_id = {} , power = {} , params = {}",
                user_id,
                power,
                params);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.areInt(user_id))
        {
            logger.error("==> string is not int type , user_id = {}  ", user_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        
        List<User> userList = null;
        try
        {
           userList = dao.loadEmpList(user_id, power);
        }
        catch (Exception e)
        {
            logger.error("==> dao.loadEmpList(user_id, power) execute error , user_id = {} , power = {}  ",
                user_id, power,
                e);
         //   e.printStackTrace();
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.loadEmpList(user_id, power) execute success , user_id = {} , power = {}  ", user_id, power);
    
        params.put("empList", userList);
    }
    
    /**
     * 员工第一次修改密码
     */
    public void updatePwdFirst(String user_id, String power, String new_pwd)
     throws WrenchElevatorInstallException
    {
        if (WrenchElevatorInstallUtil.hasNull(user_id,power, new_pwd))
        {
            logger.error("==> param is not found , tel = {} , pwd = {} , params = {} ",
                user_id,
                power,
                new_pwd);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.areInt(user_id))
        {
            logger.error("==> string is not int type , user_id = {}  ", user_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        if (!WrenchElevatorInstallUtil.isEncode(new_pwd))
        {
            logger.error("==> password is not encode , new_pwd = {} ", new_pwd);
            throw new WrenchElevatorInstallException(Result.PASSWORD_IS_NOT_ENCODE_CODE, Result.PASSWORD_IS_NOT_ENCODE_MESSAGE);
        }
        int commit = -1;
        try
        {
            commit = dao.updatePwdFirst(user_id, power, new_pwd);
        }
        catch (Exception e)
        {
            logger.error("==> dao.updatePwdFirst(user_id, power, new_pwd) execute error , user_id = {} , power = {} , new_pwd = {} ",
                user_id, power, new_pwd,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.updatePwdFirst(user_id, power, new_pwd) execute success , user_id = {} , power = {} , new_pwd = {} ", user_id, power,new_pwd);
        
        if (commit <= 0)
        {
            logger.error("==> update pwd fail , user_id = {} , power = {} , new_pwd = {} ,commit = {}",
                user_id,
                power,
                new_pwd,
                commit);
            throw new WrenchElevatorInstallException(Result.UPDATE_PWD_FAIL_CODE, Result.UPDATE_PWD_FAIL_MESSAGE);
        }
    
    }
    
    
    /**
     * 修改密码根据电话号码
     */
    public void updatePwd(String tel,String new_pwd)
        throws WrenchElevatorInstallException
       {
        if (WrenchElevatorInstallUtil.hasNull(tel, new_pwd))
        {
            logger.error("==> param is not found , user_id = {} , new_pwd = {}  ",
                tel,
                new_pwd);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.isVaildPhone(tel))
        {
            logger.error("==> phone number is not vaild , tel = {} ", tel);
            throw new WrenchElevatorInstallException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.isEncode(new_pwd))
        {
            logger.error("==> password is not encode , new_pwd = {} ", new_pwd);
            throw new WrenchElevatorInstallException(Result.PASSWORD_IS_NOT_ENCODE_CODE, Result.PASSWORD_IS_NOT_ENCODE_MESSAGE);
        }
        int commit = -1;
        try
        {
           commit = dao.updatePwd(tel, new_pwd);
        }
        catch (Exception e)
        {
            logger.error("==> dao.updatePwd(tel, new_pwd) execute error , tel = {} , new_pwd = {} ",
                tel, new_pwd,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.updatePwd(tel, power, new_pwd) execute success , tel = {} , new_pwd = {} ", tel,new_pwd);
    
        if (commit <= 0)
        {
            logger.error("==> update pwd fail , tel = {} , new_pwd = {} ,commit = {}",
                tel,
                new_pwd,
                commit);
            throw new WrenchElevatorInstallException(Result.UPDATE_PWD_FAIL_CODE, Result.UPDATE_PWD_FAIL_MESSAGE);
        }
       }
    
    
    /**
     * 添加员工
     */
    public void addEmp(String user_id,String power,String user_name,String user_pwd,String user_tel,String company_id,ModelMap params )
        throws WrenchElevatorInstallException
       {
        if (WrenchElevatorInstallUtil.hasNull(user_id, power,user_name,user_pwd,user_tel,params))
        {
            logger.error("==> param is not found , user_id = {} , power = {}  , user_name = {} ,user_name = {} , user_pwd = {} ,user_tel = {} , company_id = {} ,params = {}",
                user_id,
                power,
                user_name,
                user_pwd,
                user_tel,
                company_id,
                params);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.areInt(user_id,company_id))
        {
            logger.error("==> string is not int type , user_id = {} , company_id = {} ", user_id,company_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        if (!WrenchElevatorInstallUtil.isVaildPhone(user_tel))
        {
            logger.error("==> phone number is not vaild , user_tel = {} ", user_tel);
            throw new WrenchElevatorInstallException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.isEncode(user_pwd))
        {
            logger.error("==> password is not encode , user_pwd = {} ", user_pwd);
            throw new WrenchElevatorInstallException(Result.PASSWORD_IS_NOT_ENCODE_CODE, Result.PASSWORD_IS_NOT_ENCODE_MESSAGE);
        }
         params.put("user_id",user_id);
         params.put("power",power);
         params.put("user_name",user_name);
         params.put("user_pwd",user_pwd);
         params.put("user_tel",user_tel);
         params.put("company_id",company_id);
        
         try
         {
             dao.addEmp(params);
         }
         catch (Exception e)
         {
             logger.error("==> dao.addEmp(params) execute error , user_id = {} , power = {}  , user_name = {} ,user_name = {} , user_pwd = {} ,user_tel = {} , company_id = {} ",
                 user_id,
                 power,
                 user_name,
                 user_pwd,
                 user_tel,
                 company_id,
                 e);
             throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
         }
         logger.debug("==> dao.addEmp(params) execute error , user_id = {} , power = {}  , user_name = {} ,user_name = {} , user_pwd = {} ,user_tel = {} ", 
             user_id,
             power,
             user_name,
             user_pwd,
             user_tel,
             company_id);
         
         Object result = params.get("result");
         if (WrenchElevatorInstallUtil.objectEquals(Dict.EXIST, result))
         {
             logger.error("==> already existed  , params = {} ", params);
             throw new WrenchElevatorInstallException(Result.ALREADY_EXISTED_CODE, Result.ALREADY_EXISTED_MESSAGE);
         } 
     
        
        
       }
}
